#include"calserver.hpp"
#include"memory"
using namespace std;
using namespace server;
void calculate(request req,response *resp)
{    
    switch(req._op)
    { case '*':
        {   resp->result=req._x*req._y;
            resp->exitcode=1;
            break;
        }
      case '+':
        {   resp->result=req._x+req._y;
            resp->exitcode=1;
            break;
        }
      case '-':
       {    
          resp->result=req._x-req._y;
          resp->exitcode=1;
          break;
       }
       case '/':
       {
         if(req._y==0)
         {
            logMessage(ERROR,"div 0 error");
            resp->result=-1;
            resp->exitcode=0;
            break;
         }
          resp->result=req._x/req._y;
          resp->exitcode=1;
          break;

       }
       case '%':
       {
           if(req._y==0)
         {
            logMessage(ERROR,"mod 0 error");
            resp->result=-1;
            resp->exitcode=0;
            break;
         }
          resp->result=req._x%req._y;
          resp->exitcode=1;
          break;
       }
        default: resp->exitcode = 0; break;
   
    }
   
     



}



static void Usage(string proc)
{
    cout << "\nUsage:\n\t" << proc << "  serverport\n\n";
}
// ./tcpclient serverip serverport
int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        Usage(argv[0]);
        exit(1);
    }
    uint16_t serverport = atoi(argv[1]);

    unique_ptr<Calserver> tcli(new Calserver(serverport));
    tcli->initServer();
    tcli->start(calculate);
    return 0;
}